<?php

namespace App\Http\Controllers\Api\Service;

use Illuminate\Support\Facades\DB;

class AuthService
{


    public  function getMenuByUserId($userid)
    {
      
        $sql="SELECT * FROM sys_menu WHERE id IN (SELECT DISTINCT(menu_id) FROM sys_role_menu WHERE role_id IN
        (SELECT role_id from sys_user_role AS sur LEFT JOIN sys_role AS sr ON sur.role_id=sr.id  
        WHERE sur.user_id=$userid AND sr.forbidden != 1)) AND forbidden != 1 order BY sort asc";
        $menu=DB::select($sql);
        return objectToArray($menu);
    }


    public  function getMenuByRoleId($roleId)
    {
      
        $sql="SELECT * FROM sys_menu WHERE id IN (SELECT DISTINCT(menu_id) FROM sys_role_menu WHERE role_id = $roleId
        ) AND forbidden != 1 order BY sort asc";
        $menu=DB::select($sql);
        return objectToArray($menu);
    }

    public function getButton($button)
    {
        $data=[];
        if($button){
            foreach($button as $v1){
                $data['permission'][]=$v1['permission'];
                $data['component'][$v1['permission']]=$v1['component'];
            }
        }
        return $data;
    }
}